import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class AddEmpForm extends JFrame {
	private static final long serialVersionUID = 1L;
	JPanel adPanel;
	private JLabel fname, mname, lname, passlabel, cpaslab,  bDate, adDr, contactNo, salab;
	private JTextField firstName, middleName, lastName, contactNum, baseSal;
	private JPasswordField password, confPass;
	private JComboBox bMonth,Bday,Byear;
	private JTextArea address;
	private JButton submit, cancel;
	ImageIcon ii = new ImageIcon(this.getClass().getResource("images/addbg.png"));
	private Image bgImage = ii.getImage();
	String[] bMonthItems = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

	int[] intDay = new int[31];
	String[] BdayItems = new String[31];
	int[] intYear = new int[54];
	String ByearItems[] = new String[54];

	public AddEmpForm() {
		setTitle("Add Profile");

		adPanel = new JPanel() {
			private static final long serialVersionUID = 1L;

			protected void paintComponent(Graphics g){
				g.drawImage(bgImage, 0, 0, null);
				super.paintComponent(g);
			}
		};





		//construct components
		fname = new JLabel ("First Name:");
		mname = new JLabel ("Middle Initial:");
		lname = new JLabel ("Last Name:");
		firstName = new JTextField ();
		middleName = new JTextField ();
		lastName = new JTextField ();
		passlabel = new JLabel ("Password:");
		cpaslab = new JLabel ("Confirm Password:");
		password = new JPasswordField();
		confPass = new JPasswordField();
		bDate = new JLabel ("Birthdate:");
		bMonth = new JComboBox (bMonthItems);
		Bday = new JComboBox ();
		Byear = new JComboBox ();
		adDr = new JLabel ("Address:");
		address = new JTextArea (5, 5);
		contactNo = new JLabel ("Contact No.:");
		contactNum = new JTextField ();
		salab = new JLabel ("Base Salary:");
		baseSal = new JTextField();
		submit = new JButton ("Submit");
		cancel = new JButton ("Cancel");

		InitializeDrop(new Integer(31));
		InitializeYear();

		//set components properties
		bMonth.setToolTipText ("Month");
		Bday.setToolTipText ("Day");
		Byear.setToolTipText ("Year");

		//adjust size and set layout
		setPreferredSize (new Dimension (319, 428));
		setLayout (null);
		setResizable(false);

		//add components
		adPanel.add (fname);
		adPanel.add (mname);
		adPanel.add (lname);
		adPanel.add (bDate);
		adPanel.add (firstName);
		adPanel.add (middleName);
		adPanel.add (lastName);
		adPanel.add (bMonth);
		adPanel.add (Bday);
		adPanel.add (Byear);
		adPanel.add (adDr);
		adPanel.add (address);
		adPanel.add (contactNo);
		adPanel.add (contactNum);
		adPanel.add (passlabel);
		adPanel.add (password);
		adPanel.add (confPass);
		adPanel.add (cpaslab);
		adPanel.add (salab);
		adPanel.add (baseSal);
		adPanel.add (submit);
		adPanel.add (cancel);
		add(adPanel);

		adPanel.setSize(319,428);
		adPanel.setOpaque(false);
		adPanel.setLayout(null);
		adPanel.setVisible(true);

		//set component bounds (only needed by Absolute positioning)
		fname.setBounds (15, 70, 70, 25);
		mname.setBounds (15, 100, 85, 25);
		lname.setBounds (15, 130, 70, 25);
		firstName.setBounds (90, 70, 100, 25);
		middleName.setBounds (90, 100, 100, 25);
		lastName.setBounds (90, 130, 100, 25);
		bDate.setBounds (15, 240, 60, 25);
		bMonth.setBounds (75, 240, 90, 25);
		Bday.setBounds (170, 240, 55, 25);
		Byear.setBounds (230, 240, 80, 25);
		adDr.setBounds (15, 270, 55, 25);
		address.setBounds (75, 275, 160, 55);
		contactNo.setBounds (15, 335, 75, 25);
		contactNum.setBounds (90, 335, 100, 25);
		submit.setBounds (85, 370, 110, 25);
		cancel.setBounds (195, 370, 100, 25);
		passlabel.setBounds (15, 160, 115, 25);
		cpaslab.setBounds (15, 185, 115, 25);
		salab.setBounds (15, 210, 145, 25);
		password.setBounds (90, 160, 100, 25);
		confPass.setBounds (125, 185, 100, 25);
		baseSal.setBounds (90, 210, 100, 25);

		fname.setForeground(Color.WHITE);
		mname.setForeground(Color.WHITE);
		lname.setForeground(Color.WHITE);
		bDate.setForeground(Color.WHITE);
		passlabel.setForeground(Color.WHITE);
		cpaslab.setForeground(Color.WHITE);
		adDr.setForeground(Color.WHITE);
		contactNo.setForeground(Color.WHITE);
		passlabel.setForeground(Color.WHITE);
		submit.setForeground(Color.WHITE);
		cancel.setForeground(Color.WHITE);
		submit.setBackground(Color.BLACK);
		cancel.setBackground(Color.BLACK);
		salab.setForeground(Color.WHITE);


		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setVisible(false);
			}
		});

		submit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){


				try{
					int goAdd = JOptionPane.showConfirmDialog(adPanel, "Are you sure you want to add this employee in the database", "Confirm" , JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
					if(goAdd == JOptionPane.YES_OPTION)
					{
						String bday = getByear() + "-" + getbMonth() + "-" + getBday();
						if(validateForm() == true)
							if(getConfirmPassword().equals(getPassword()))
							{	
								ConnectionFactory myFactory = ConnectionFactory.getFactory();
								Connection conn = myFactory.getConnection();

								PreparedStatement s;
								s = conn.prepareStatement (
										"INSERT INTO employee (firstName, middleInitial, lastName, password, bDate, address, contactNum, accountType, baseSalary, status) VALUES(?,?,?,?,?,?,?,'1',?,'Active')");
								s.setString (1, getFirstName());
								s.setString (2, getMiddleName());
								s.setString (3, getLastName());
								s.setString (4, getPassword());
								s.setString (5, bday);
								s.setString (6, getAddress());
								s.setString (7, getContactNum());
								s.setString (8, getBaseSal());

								s.executeUpdate();
								conn.close ();

								setVisible(false);
								JOptionPane.showMessageDialog(adPanel, "You have successfully added a new profile.");
								AdminForm.goToPage(0);	
							}
							else
								JOptionPane.showMessageDialog(adPanel, "Password mismatch.", "Error", JOptionPane.ERROR_MESSAGE);
						else
							JOptionPane.showMessageDialog(adPanel, "All fields must be filled out!", "Error", JOptionPane.ERROR_MESSAGE);
					}

				}catch(Exception ex){

					ex.printStackTrace(); 
				}
			}
		});


		bMonth.addActionListener(new ActionListener() {
			public void actionPerformed (ActionEvent e){

				boolean leap = false;

				leap = CheckLeap(Integer.parseInt(Byear.getSelectedItem().toString()));

				if(bMonth.getSelectedIndex() == 1)
				{
					if(leap==false)
						InitializeDrop(28);
					else
						InitializeDrop(29); 
				}
				else if (bMonth.getSelectedIndex() == 0 || bMonth.getSelectedIndex() == 2 || bMonth.getSelectedIndex() == 4 
						|| bMonth.getSelectedIndex() == 6 || bMonth.getSelectedIndex() == 7 || bMonth.getSelectedIndex() == 9
						|| bMonth.getSelectedIndex() == 11)
					InitializeDrop(31);
				else if (bMonth.getSelectedIndex() == 3 || bMonth.getSelectedIndex() == 5 || bMonth.getSelectedIndex() == 8
						|| bMonth.getSelectedIndex() == 10)
					InitializeDrop(30);
			}
		}
		);

		Byear.addActionListener(new ActionListener() {
			public void actionPerformed (ActionEvent e){

				boolean leap = false;

				leap = CheckLeap(Integer.parseInt(Byear.getSelectedItem().toString()));

				if(bMonth.getSelectedIndex() == 1)
				{
					if(leap==false)
						InitializeDrop(28);
					else
						InitializeDrop(29); 
				}
			}
		}
		);


	}

	public void InitializeDrop(int ctr) {

		Bday.removeAllItems();

		for(int a = 1; a <= ctr; a++ )
		{     
			intDay[a-1] = a;
			BdayItems[a-1] = Integer.toString(intDay[a-1]);
			Bday.addItem(BdayItems[a-1]);
		}

		Bday.repaint();


	}

	public void InitializeYear() {

		for(int a = 1940; a <= 1993; a++)
		{     
			int index = 0;
			intYear[index] = a;
			ByearItems[index] = Integer.toString(intYear[index]);
			Byear.addItem(ByearItems[index]);
			index++;
		}

	}

	public boolean CheckLeap(int year) {
		if(year%4 == 0)
		{
			if(year%100 != 0)
			{
				return true;
			}
			else
			{
				if(year%400 == 0)
					return true;
				else
					return false;
			}
		}
		return false;
	}

	public String getFirstName() {
		return firstName.getText().trim();
	}

	public String getMiddleName() {
		return middleName.getText().trim();
	}

	public String getLastName() {
		return lastName.getText().trim();
	}

	public String getContactNum() {
		return contactNum.getText().trim();
	}

	public String getPassword() {
		return new String(password.getPassword());
	}

	public String getConfirmPassword() {
		return new String(confPass.getPassword());
	}

	public int getbMonth() {
		String mon = bMonth.getSelectedItem().toString();
		int con = 0;
		if(mon.equals("January"))
			con = 1;
		else if(mon.equals("February"))
			con = 2;
		else if(mon.equals("March"))
			con = 3;
		else if(mon.equals("April"))
			con = 4;
		else if(mon.equals("May"))
			con = 5;
		else if(mon.equals("June"))
			con = 6;
		else if(mon.equals("July"))
			con = 7;
		else if(mon.equals("August"))
			con = 8;
		else if(mon.equals("September"))
			con = 9;
		else if(mon.equals("October"))
			con = 10;
		else if(mon.equals("November"))
			con = 11;
		else
			con = 12;

		return con;
	}

	public String getBday() {
		return Bday.getSelectedItem().toString();
	}

	public String getByear() {
		return Byear.getSelectedItem().toString();
	}

	public String getAddress() {
		return address.getText().trim();
	}

	public String getBaseSal(){
		return baseSal.getText().trim();
	}

	public boolean validateForm(){
		if(getFirstName().length()==0 || getMiddleName().length() == 0 || getLastName().length()==0 || getPassword().length()==0 || getConfirmPassword().length()==0 || getAddress().length() == 0 || getContactNum().length()==0 || getBaseSal().length()==0)
			return false;
		else
			return true;
	}
}
